package com.sgb.zhaocai.viewmodel;

import androidx.lifecycle.ViewModel;

import com.sgb.zhaocai.base.BaseApi;
import com.sgb.zhaocai.entity.MemberCompanyEntitly;
import com.sgb.zhaocai.view.fragment.PickMemberPersonalFragment;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PickMemberPersonalModel  extends ViewModel {
    PickMemberPersonalFragment fragment;
    private boolean isLastPage;
    private int page = 1;

    public void setActivity(PickMemberPersonalFragment pickMemberPersonalFragment) {
        this.fragment = pickMemberPersonalFragment;
    }
    /**
     * 会员单位-个人列表
     */
    public void getMixMemberListByPage(boolean isLoadMore,String name,String memberTypeNo,String membergrade) {
        page = isLoadMore? page+1:1;
        Map map = new HashMap();
        map.put("limit","20");
        map.put("page",page);
        map.put("memberType","2");//会员主体:1企业 2个人
        map.put("relation","2");
        map.put("status","2");
        map.put("name",name);
        map.put("memberCategory",memberTypeNo);
        map.put("starsCope",membergrade);
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getMixMemberListByPage(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<MemberCompanyEntitly>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<MemberCompanyEntitly>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    isLastPage = data.getData().getCurrentPage() >= data.getData().getPageSize();
                    for (MemberCompanyEntitly memberCompanyEntitly : data.getData().getList()) {
                        memberCompanyEntitly.setCheck(false);
                    }
                    fragment.setMixMemberListByPage(data.getData().getList(),isLastPage);
                }else {
                    MToast.showToast(fragment.getActivity(),data.getMsg());
                }
            }
        });
    }

    /**
     * 会员等级
     */
    public void getMemberGrade() {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getMemberGrade().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    fragment.setMemberGradeList(data.getData());
                }else {
                    MToast.showToast(fragment.getActivity(),data.getMsg());
                }
            }
        });
    }
    /**
     * 会员类型
     */
    public void getMemberTypeList() {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getMemberTypeList("0").enqueue(new ICallBack<BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    fragment.setMemberTypeList(data.getData());
                }else {
                    MToast.showToast(fragment.getActivity(),data.getMsg());
                }
            }
        });
    }
}
